package ouc.pkere.mysqlmanager.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/mysql")
public class MySQLController {

    @RequestMapping(value = "/connect", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> connect(@RequestBody String str) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, String> map = JSONObject.parseObject(str, Map.class);
        Connector connector = new Connector();
        String message = connector.connect(map.get("ip"), map.get("port"), map.get("username"), map.get("password"),
                map.get("dbName"));
        result.put("message", message);
        connector.disconnect();
        return result;
    }

    @RequestMapping(value = "/tables", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> showTables(@RequestBody String str) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, String> map = getDBConfig(resquestParams);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            Connector connector = new Connector();
            connector.connect(map.get("ip"), map.get("port"), map.get("username"), map.get("password"), map.get("dbName"));
            ArrayList<String> tableName = connector.showTables();
            result.put("data", tableName);
            connector.disconnect();
            return result;
        }
    }

    @RequestMapping(value = "/tables/{tableName}", produces = {
            "application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> showColumnType(@PathVariable String tableName, @RequestBody String str)
            throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, String> map = getDBConfig(resquestParams);

        if (map == null || map.isEmpty()) {
            return null;
        } else {
            Connector connector = new Connector();
            connector.connect(map.get("ip"), map.get("port"), map.get("username"), map.get("password"), map.get("dbName"));
            Map<String, String> columnType = connector.showColumnType(tableName, " * ");
            result.put("data", columnType);
            connector.disconnect();
            return result;
        }
    }

    @RequestMapping(value = "/select", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> sqlSelect(@RequestBody String str) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, String> map = getDBConfig(resquestParams);
        String sql = (String) resquestParams.get("sql");
/*		Integer maxRows = (Integer) resquestParams.get("maxRows");
		if(maxRows == null){
			maxRows = 0;
		}
*/
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            Connector connector = new Connector();
            connector.connect(map.get("ip"), map.get("port"), map.get("username"), map.get("password"), map.get("dbName"));
            ArrayList<Map<String, String>> rows = connector.sqlSelect(sql, 5);
            result.put("data", rows);
            connector.disconnect();
            return result;
        }
    }

    @RequestMapping(value = "/update", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> sqlUpdate(@RequestBody String str) throws Exception {
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, String> map = getDBConfig(resquestParams);
        if (map == null || map.isEmpty()) {
            return null;
        } else {
            String sql = (String) resquestParams.get("sql");
            Connector connector = new Connector();
            connector.connect(map.get("ip"), map.get("port"), map.get("username"), map.get("password"), map.get("dbName"));
            String message = connector.sqlUpdate(sql);
            result.put("message", message);
            connector.disconnect();
            return result;
        }
    }

    private Map<String, String> getDBConfig(Map<String, Object> requestParams) {
        Map<String, String> map = new HashMap<>();
        if (requestParams == null) {
            return null;
        } else {
            String ip = (String) requestParams.get("ip");
            String port = (String) requestParams.get("port");
            String username = (String) requestParams.get("username");
            String password = (String) requestParams.get("password");
            String dbName = (String) requestParams.get("dbName");
            map.put("ip", ip);
            map.put("port", port);
            map.put("username", username);
            map.put("password", password);
            map.put("dbName", dbName);
            return map;
        }
    }

}
